using System;
using System.Collections.Generic;
using System.Text;

namespace Weazel.Microblaze.Simulator.Components.Microblaze.Instructions
{
    /// <summary>
    /// Move from special purpose register
    /// </summary>
    public class Mfs : Instruction
    {
        private uint specialPurposeRegister;

        public override int CycleCount
        {
            get { return 1; }
        }

        public Mfs()
            : base("mfs")
        {
        }

        public override void Set(uint address, uint value)
        {
            base.Set(address, value);
            specialPurposeRegister = value & 0x3;
        }

        /// <summary>
        /// Copies the contents of the special purpose register rS into 
        /// register rD.
        /// </summary>    
        public override void Execute(MicroblazeComponent MicroblazeComponent)
        {
            uint specialPurposeRegisterContent =
              MicroblazeComponent.ReadSpecialPurposeRegister(specialPurposeRegister);

            MicroblazeComponent.WriteRegister(
              DestinationRegister,
              specialPurposeRegisterContent,
              Address
            );
        }

        public override string ToString()
        {
            return String.Format("mfs r{0}, r{1}",
              DestinationRegister,
              specialPurposeRegister);
        }
    }
}
